﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Configuration;

namespace Infrastructure.CloudWeb.BusinessServices
{
    public class MethodAuditService : IMethodAuditService
    {
        private static CloudStorageAccount storageAccount;
        private Model.InfrastructureModel model;
        static MethodAuditService()
        {

            storageAccount = CloudStorageAccount.FromConfigurationSetting("InfrastructureConnectionString");
            CloudTableClient.CreateTablesFromModel(typeof(Model.InfrastructureModel), storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials); 
            
        }
        public MethodAuditService()
        {
            this.model = new Model.InfrastructureModel(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
            this.model.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(3));
        }
        public IEnumerable<Entities.MethodAudit> GetAll()
        {
            var results = from c in this.model.MethodAudits select c;
            return results.ToList(); 
        }
        public void AddMethodAudit(Entities.MethodAudit item)
        {
            item.PartitionKey = ConfigurationManager.AppSettings["ApplicationName"];
            //item.RowKey = Guid.NewGuid().ToString();            

            //item.Activity = Guid.NewGuid();
            //item.Application = "test";
            //item.ClassDescription = "test";
            ////item.Created = DateTime.Now;
            //item.Description = "test";            
            //item.Method = "test";
            //item.Module = "test";
            //item.Name = "test";
            //item.Owner = "test";            
            
            //item.Session = Guid.NewGuid();
            //item.Type = "test";
            //item.UserName = "test";           

            this.model.AddObject("MethodAudits",item);
            this.model.SaveChanges();
        }
    }
}
